//-------------------------------------------------------------------------------------
//autoisland
function autoisland()
{
	if (location.href.indexOf('index.php')!=-1) {
		//check indexpage for resource buildup.
	
		var widgetdivs = document.evaluate("//div[@class='widget_title']",document,null,XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,null);
		for (var i=0;i<widgetdivs.snapshotLength;i++) {
			if (widgetdivs.snapshotItem(i).innerHTML.indexOf('YER ISLAND')!=-1) {
				var island=widgetdivs.snapshotItem(i).parentNode;
				break;
			}
		}
	
		var islandrows = document.evaluate(".//span",island,null,XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,null);
		for (var j=0;j<islandrows.snapshotLength;j++) {
			var wood = parseInt(islandrows.snapshotItem(1).innerHTML);
			var stone = parseInt(islandrows.snapshotItem(2).innerHTML);
			var iron = parseInt(islandrows.snapshotItem(3).innerHTML);
		}
	
		if ((wood>=1)||(stone>=1)||(iron>=1)) {
			addToLog('Island: '+wood+' Wood, '+stone+' Stone and '+iron+' Iron available.');
			document.location = 'http://apps.facebook.com/'+SCRIPT.name+'/homebase.php';
			flag=1;
		}
	}
	
	if (location.href.indexOf('homebase.php')!=-1) {
		//Log stored resources & hiring friends.
		var msg = document.evaluate("//span[@class='message_body']",document,null,9,null).singleNodeValue;
		if (msg!=null) {
			if (msg.innerHTML.indexOf('You have succesfully stored')!=-1) {
				//You have succesfully stored 17 wood!
				addToLog(msg.innerHTML.replace('You have succesfully stored','Island: Stored').replace('!','.'));
			} else if (msg.innerHTML.indexOf('You have hired')!=-1) {
				//You have hired Anthony Allegretti to gather 24 wood over 12 hours.
				addToLog('Hired '+msg.innerHTML.split('You have hired ')[1].split(' hours.')[0]+' hours.');
			}
		}
		
		//Check if on friend selection page
		document.addEventListener("DOMNodeInserted",chooseFriend,false);
		
		//Hire workers or store resources.
		var islandforms = document.evaluate("//form[@id='app16421175101_hb_2']",document,null,XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,null);
		var hireworkers = document.evaluate("//form[@id='app16421175101_hb_1']",document,null,XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,null);
		if (islandforms.snapshotLength>0) {
			for (var i=0;i<islandforms.snapshotLength;i++) {
				islandforms.snapshotItem(i).submit();
				break;
			}
		} else if (hireworkers.snapshotLength>0) {
			for (var i=0;i<hireworkers.snapshotLength;i++) {
				hireworkers.snapshotItem(i).submit();
				break;
			}
		} else {
			//alert('removing listener and returning to index');
			document.removeEventListener("DOMNodeInserted",chooseFriend,false);
			document.location = 'http://apps.facebook.com/'+SCRIPT.name+'/index.php';
		}
	}
}